﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Light_Notes
{
    /// <summary>
    /// Interaction logic for NotesTab.xaml
    /// </summary>
    public partial class NotesTab : TabItem
    {
        private Light_Notes.MainWindow mainWindow;

        public NotesTab()
            : base()
        {
            InitializeComponent();

            mainWindow = App.Current.MainWindow as Light_Notes.MainWindow;
            Rename();
        }

        public void FindNext(string text, FindOptions findOptions)
        {
            FindNext(text, findOptions, false);
        }

        public void FindIncremental(string text, FindOptions findOptions)
        {
            FindNext(text, findOptions, true);
        }

        public void FindNext(string text, FindOptions findOptions, bool incremental)
        {
            TextPointer position = incremental ? NoteBox.Selection.Start : NoteBox.Selection.End;
            TextRange textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Forward);
            if (textRange != null) NoteBox.Selection.Select(textRange.Start, textRange.End);
            else
            {
                position = NoteBox.Document.ContentStart;
                textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Forward);
                if (textRange != null) NoteBox.Selection.Select(textRange.Start, textRange.End);
                else NoteBox.Selection.Select(NoteBox.Selection.Start, NoteBox.Selection.Start);
            }
        }

        public void FindPrevious(string text, FindOptions findOptions)
        {
            TextPointer position = NoteBox.Selection.Start;
            TextRange textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Backward);
            if (textRange != null) NoteBox.Selection.Select(textRange.Start, textRange.End);
            else
            {
                position = NoteBox.Document.ContentEnd;
                textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Backward);
                if (textRange != null) NoteBox.Selection.Select(textRange.Start, textRange.End);
                else NoteBox.Selection.Select(NoteBox.Selection.Start, NoteBox.Selection.Start);
            }
        }

        public void Replace(string text, string replacement, FindOptions findOptions)
        {
            TextPointer position = NoteBox.Selection.Start;
            TextRange textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Forward);
            if (textRange != null &&
                NoteBox.Selection.Start.CompareTo(textRange.Start) == 0 &&
                NoteBox.Selection.End.CompareTo(textRange.End) == 0)
                NoteBox.Selection.Text = replacement;
            FindNext(text, findOptions, false);
        }

        public void ReplaceAll(string text, string replacement, FindOptions findOptions)
        {
            NoteBox.BeginChange();
            TextPointer position = NoteBox.Document.ContentStart;
            TextRange textRange;
            int count = 0;
            while (true)
            {
                textRange = FindHelper.FindText(position, text, findOptions, LogicalDirection.Forward);
                if (textRange == null) break;
                position = textRange.End;
                NoteBox.Selection.Select(textRange.Start, textRange.End);
                NoteBox.Selection.Text = replacement;
                count++;
            }
            NoteBox.EndChange();
            MessageBox.Show(count + " occurance(s) replaced.", "Replace All", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void rename_Click(object sender, RoutedEventArgs e)
        {
            Rename();
        }

        private void TabItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.GetType() == typeof(TextBlock) ||
                e.OriginalSource.GetType() == typeof(Border)) Rename();
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.DeleteTab(this);
        }

        private void new_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.NewTab();
        }

        private void Rename()
        {
            string oldHeader = Header as string;
            TextBox tb = new TextBox();
            tb.Text = oldHeader;
            tb.ContextMenu = null;
            tb.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.Enter:
                        Header = tb.Text;
                        mainWindow.FlagChange();
                        break;
                    case Key.Escape:
                        Header = oldHeader;
                        break;
                }
            };
            tb.LostKeyboardFocus += delegate(object sender, KeyboardFocusChangedEventArgs e)
            {
                if (Header == tb)
                {
                    Header = tb.Text;
                    mainWindow.FlagChange();
                }
            };
            tb.IsVisibleChanged += delegate(object sender, DependencyPropertyChangedEventArgs e)
            {
                tb.Focus();
            };
            Header = tb;
            tb.SelectAll();
        }

        private void NoteBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            mainWindow.FlagChange();
        }
    }
}
